package edu.scau.hibernate.ui;

import javax.swing.JFrame;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import edu.scau.hibernate.model.User;
import edu.scau.hibernate.service.UserService;
import edu.scau.hibernate.service.UserServiceImpl;

public class UpdateUser extends JFrame{
	private JLabel label1,label2,label3;
	JLabel name;
	private JPasswordField password,password2;
	private JButton yes,cancel;
	private JPanel p1,p2,p3;
	
	User user;
	
	public UpdateUser(){
		label1=new JLabel("用户名:");
		label2=new JLabel("新密码:");
		label3=new JLabel("确认密码:");
		name=new JLabel();
		password=new JPasswordField(15);
		password2=new JPasswordField(15);
		yes=new JButton("确定");
		cancel=new JButton("取消");
		
		p1=new JPanel();
		p1.setLayout(new GridLayout(3,1,10,10));
		p1.add(label1);
		p1.add(label2);
		p1.add(label3);
		
		p2=new JPanel();
		p2.setLayout(new GridLayout(3,1,10,10));
		p2.add(name);
		p2.add(password);
		p2.add(password2);
		
		p3=new JPanel();
		p3.add(yes);
		p3.add(cancel);
		
		setLayout(new BorderLayout(20,15));
		add(p1,BorderLayout.WEST);
		add(p2,BorderLayout.CENTER);
		add(p3,BorderLayout.SOUTH);
		
		setTitle("修改密码");
		setSize(320,220);
		setLocation(530,280);
		
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setVisible(false);
			}
		});
		
		yes.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(String.valueOf(password.getPassword()).equals(String.valueOf(password2.getPassword()))){
					try {
						UserService service=new UserServiceImpl();
						user.setPassword(String.valueOf(password.getPassword()));
						if(service.updateUser(user))
							JOptionPane.showMessageDialog(null, "修改成功！");
						setVisible(false);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
				else
					JOptionPane.showMessageDialog(null, "请再次确认密码！");
			}
		});
		
	}
	
	public void setUser(User user){
		this.user=user;
	}

}